package com.morizhang.merchanmanager.api.utils;

import com.morizhang.merchanmanager.api.dao.RoleAuthorityDao;
import com.morizhang.merchanmanager.api.dao.UserDao;
import com.morizhang.merchanmanager.api.model.RoleAuthorityModel;
import com.morizhang.merchanmanager.api.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ServiceAuthorityChecker {
    private static RoleAuthorityDao roleAuthorityDao;
    private static UserDao userDao;

    @Autowired
    public void setRoleAuthorityDao(RoleAuthorityDao roleAuthorityDao) {
        ServiceAuthorityChecker.roleAuthorityDao = roleAuthorityDao;
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        ServiceAuthorityChecker.userDao = userDao;
    }

    public static boolean checkAuthority(String userID, String serviceName) {
        UserModel searchResult = userDao.queryUserInfoByUserID(userID);
        if (searchResult == null)
            return false;
        RoleAuthorityModel roleAuthorityModel = new RoleAuthorityModel();
        roleAuthorityModel.setAuthorityService(serviceName);
        roleAuthorityModel.setUserRole(searchResult.getUserRole());
        int authorityCheck = roleAuthorityDao.checkAuthority(roleAuthorityModel);
        return authorityCheck != 0;
    }
}
